---
title: "Exploratory Data Analysis"
output: html_document
---

```{r setup, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```


# Univariate Plots Section
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
data = read_csv("Redwine.csv")
data <- within(data, {
  quality <- ordered(quality)
})
dim(data)
```


```{r}
str(data)
```


```{r}
summary(data)
```

Our dataset has 12 variables with 1599 observations.


```{r, message=FALSE, warning=FALSE}
library(ggplot2)
ggplot(data, aes(x = alcohol)) +
    geom_histogram()
```

```{r}
summary(data$alcohol)
```

The graph shows the distribution of alcohol percentage in wines. Most wines have an alcohol percentage of between 9 to 11%.


```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = pH)) +
    geom_histogram()
```

```{r}
summary(data$pH)
```

The graph shows the distribution of pH in wines. Most wines have a pH of between 3.0 to 3.5.


```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = residual.sugar)) +
    geom_histogram()
```

```{r}
summary(data$residual.sugar)
```

The graph shows the distribution of residual sugar in wines. The highest residual sugar in the wines is 15.5 g/dm^3.


```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = chlorides)) +
    geom_histogram()
```

```{r}
summary(data$chlorides)
```

The graph shows the distribution of chlorides in wines. Most wines have a low amount of salt.


```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = volatile.acidity)) +
    geom_histogram()
```

```{r}
summary(data$volatile.acidity)
```

The graph shows the distribution of volatile acidity in wines. Most wines have a low volatile acidity below 1.2.

```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = density)) +
    geom_histogram()
```

```{r}
summary(data$density)
```

The graph shows the distribution of density in wines. The wines data seem to have a normal distribution of alcohol density .

```{r}
ggplot(data, aes(x = quality)) +
    geom_bar()
```

```{r}
summary(data$quality)
```

The graph shows the distribution of quality in the wines. Most wines had a quality of 5 and 6.


# Univariate Analysis
## What is the structure of your dataset?
There are 1599 observations in our dataset with 12 variables (fixed acidity, volatile acidity, citric acidity, residual sugar, chlorides, free sulphur dioxide, total sulphur dioxide, density, pH, sulphates, alcohol, quality). The categorical variable is quality.

Other observations:

* Majority of wines have a density of between 0.995 and 1.000.

* Most wines have a volatile acidity of less than 0.8 g/dm^3 hence they have pleasant taste.

* Most wines have a small amount of salt less than 0.2 g/dm^3.

* The mean alcohol percentage of the wines is 10%.

## What is the main feature of interest in your dataset?
The main feature of interest in this dataset is quality which is the quality level of the wine. I want to know which features of wine influence quality and can be used to predict the quality level of a wine.

## What other features in the dataset do you think will help support your investigation into your feature(s) of interest?
Alcohol, pH, residual sugar, chlorides, and fixed acidity are features that may influence fuel consumption of a car. 


# Bivariate Plots Section
```{r}
df <- data[, c(-12)]
rs = cor(df)
round(rs, 2)
```


```{r, message=FALSE, warning=FALSE}
library(corrplot)
corrplot(rs, type = "upper", order = "hclust", 
         tl.col = "black", tl.srt = 45)
```

The plot shows the correlation plots of the variables.


```{r}
ggplot(data, aes(fixed.acidity, citric.acid)) +
    geom_point(alpha = 0.3, size = 1)
```

```{r}
cor(data$fixed.acidity, data$citric.acid)
```

The scatter is plotted to show the relationship between fixed acidity and citric acid. There is a positive relationship between the two variables.

```{r}
ggplot(data, aes(fixed.acidity, density)) +
    geom_point(alpha = 0.3, size = 1) 
```

```{r}
cor(data$fixed.acidity, data$density)
```

The scatter is plotted to show the relationship between fixed acidity and density. There is a positive relationship between the two variables.


```{r}
ggplot(data, aes(pH, fixed.acidity)) +
    geom_point(alpha = 0.3, size = 1)
```

```{r}
cor(data$fixed.acidity, data$pH)
```

The scatter is plotted to show the relationship between fixed acidity and pH. There is a negative relationship between the variables.


```{r}
ggplot(data, aes(volatile.acidity, citric.acid)) +
    geom_point(alpha = 0.3, size = 1)
```

```{r}
cor(data$citric.acid, data$volatile.acidity)
```

The scatter is plotted to show the relationship between volatile acidity and citric acid. There is a negative relationship between the variables.


```{r}
ggplot(data, aes(quality,alcohol)) +
    geom_boxplot() +
    labs(x = "quality",
         y = "alcohol")
```

```{r}
tapply(data$alcohol, data$quality, summary)
```

The boxplots is plotted to show the distribution of alcohol against quality. wines with quality level of 5 has outliers were the most wines in this level have low alcohol percentage but some of the wines show a higher alcohol percentage.


```{r}
ggplot(data, aes(quality,pH)) +
    geom_boxplot() +
    labs(x = "quality",
         y = "pH")
```

```{r}
tapply(data$pH, data$quality, summary)
```

The boxplots is plotted to show the distribution of pH against quality. wines with quality level of 3 have high pH while wines with quality level of 8 have the lowest pH level. Wines with quality have outliers in both ends meaning some of the wine have high and low pH than the normal range.


```{r}
ggplot(data, aes(quality,residual.sugar)) +
    geom_boxplot() +
    labs(x = "quality",
         y = "residual.sugar")
```

```{r}
tapply(data$residual.sugar, data$quality, summary)
```

The boxplots is plotted to show the distribution of residual sugar against quality. wines with quality level of 3, 4, and 8 have the lowest median of residual sugar. WInes with quality level of 5 and 6 have alot of outliers meaning they had more sugar than the normal range in those quality levels.


```{r}
ggplot(data, aes(quality,volatile.acidity)) +
    geom_boxplot() +
    labs(x = "quality",
         y = "volatile.acidity")
```

```{r}
tapply(data$volatile.acidity, data$quality, summary)
```

The boxplots is plotted to show the distribution of volatile acidity against quality. wines with quality level of 3 have a high level of volatile acidity while wine with quality level of 8 have a low level of volatile acidity. WInes with quality level of 5 and 6 have alot of outliers meaning they had more volatile acidity than the normal range in those quality levels.


# Bivariate Analysis
## Did you observe any interesting relationships between the other features (not the main feature(s) of interest)?
The negative relationship between pH and citric was interesting. Another interesting relationship was the negative relationship between citric acid and volatile acidity.


## What were the strongest relationships you found?
There was a strong relationships between fixed acidity and citric acid, fixed acidity and density, and free sulphur dioxide and total sulphur dioxide. 


# Multivariate Plots Section
```{r}
ggplot(data = data, mapping = aes(x = alcohol, y = ..density..)) +
  geom_freqpoly(mapping = aes(colour = quality), binwidth = 0.3)
```

This chart shows distribution of alcohol by quality of the wines.


```{r}
ggplot(data = data, mapping = aes(x = pH, y = ..density..)) +
  geom_freqpoly(mapping = aes(colour = quality), binwidth = 0.1)
```

This chart shows distribution of pH by quality of the wines.


```{r}
ggplot(data = data, mapping = aes(x = residual.sugar, y = ..density..)) +
  geom_freqpoly(mapping = aes(colour = quality), binwidth = 0.2)
```

This chart shows distribution of residual sugar by quality of the wines.


```{r}
mlp <- ggplot(data, aes(x=pH,y=citric.acid/pH)) + geom_point(shape=1)
mlp
#mlp + facet_grid(quality~ .)
```

This chart shows the relationship between pH levels and Citric acid/residual sugar.


# Multivariate Analysis
## Talk about some of the relationships you observed in this part of the investigation.
Wines with a moderate pH have a low citric acid/pH rate. Wines with quality level have a high alcohol level compared to the rest. Wines with quality level of 3 and 8 have a high residual sugar.  


# Final Plots and Summary
## Plot One
```{r, message=FALSE, warning=FALSE}
ggplot(data, aes(x = residual.sugar)) +
    geom_histogram() +
   labs(title = "Distribution of Residual Sugar",
        x = "Residual sugar (g/dm^3)",
        y = "Number of wines") + 
   theme_minimal()
```

## Description One
The distribution of residual sugar of the wines seem to skew to the right. Most wines have a residual sugar of below 4 g/dm^3.


## Plot Two
```{r}
ggplot(data, aes(quality,alcohol)) +
    geom_boxplot() +
    labs(title = "Quality of wines by alcohol",
         x = "Quality level",
         y = "Alcohol(%)")
```


## Description Two
Wines with quality level of 5 had a low median alcohol percentage. But some wines with quality level of 5 seemed to have high alcohol percentage hence appearing as outliers. Wines with quality level of 8 have a high median alcohol percentage.


## Plot Three
```{r}
mlp <- ggplot(data, aes(x=pH,y=citric.acid/residual.sugar)) + geom_point(shape=1) +
  labs(title = "pH Distribution by Citric acid/Residual sugar",
         x = "pH level",
         y = "Citric acid/Residual sugar (g/dm^3)")
mlp
```

## Description Three
The scatter plot shows a negative relationship. The citric acid/residual sugar has a negative correlation with pH levels of the wines. Wines with low pH tend to have a high citric acid/residual sugar. As the pH levels increase the citric acid/residual sugar decreases.


# Reflection

The wineQualityReds dataset contained information of about 1599 wines. The dataset comprises of information about wines and their features. I did some exploration to understand the variables in the dataset. I explored relationship between the quality of wines with other variables.

There was a good relationship between the fixed acidity and citric acid. A moderate relationship was also observed between fixed acidity and density The surprising thing was that pH and citric acid had a negative correlation despite them being closely related.

The limitation of this dataset is it had red wines observations only. The dataset did not also include other types of wines wich as white wine. Give the dataset contains data from 2009 the analysis of wines would not reflect on the wines produced from 2010 to 2021.



